﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

using DragDraw.Util;
using DragDraw.UserService;

namespace DragDraw
{
    public partial class FriendControl : UserControl, IUserServiceListener
    {
        private const int FRIEND_PANEL = 1;
        private const int FIND_PANEL = 2;
        private List<UserListData> m_friends = new List<UserListData>();
        private List<UserListData> m_allUsers = new List<UserListData>();
        
        private int m_index = -1;
        private int m_status = 1;

        public FriendControl()
        {
            InitializeComponent();
        }

        public void Init()
        {
            MsgPanel.Visibility = Visibility.Collapsed;

            m_friends.Clear();
            m_allUsers.Clear();
            foreach (string id in OperationManager.Instance.MyFriends)
            {
                UserListData uld = new UserListData();
                uld.ID = id;
                uld.NickName = OperationManager.Instance.AllUsers[id].Realname;
                m_friends.Add(uld);
            }
            foreach (string id in OperationManager.Instance.AllUsers.Keys)
            {
                UserListData uld = new UserListData();
                uld.ID = id;
                uld.NickName = OperationManager.Instance.AllUsers[id].Realname;
                m_allUsers.Add(uld);
            }
            btnFriend_Click(this, null);
        }

        public void OnGetUserInfo(GetUserInfoCompletedEventArgs e)
        {
            UserListData f = new UserListData();
            f.ID = e.Result.UserID;
            f.NickName = e.Result.Realname;
            m_friends.Add(f);

            listFriend.ItemsSource = m_friends.ToArray();
        }
        public void OnSetUserInfo(SetUserInfoCompletedEventArgs e) { }
        public void OnSetPassword(SetPasswordCompletedEventArgs e) { }
        public void OnGetUserList(GetUserListCompletedEventArgs e) { }

        private void ShowPanel()
        {
            if (m_status == FRIEND_PANEL)
            {
                BtnSend.Content = "发送消息";
            }
            else
            {
                if (OperationManager.Instance.MyFriends.Contains(m_allUsers[m_index].ID))
                    BtnSend.Content = "已经是好友了";
                else if (OperationManager.Instance.MyUserInfo.UserID == m_allUsers[m_index].ID)
                    BtnSend.Content = "这是你自己";
                else
                    BtnSend.Content = "加为好友";
            }
            MsgPanel.Visibility = Visibility.Visible;
        }

        private void btnFriend_Click(object sender, RoutedEventArgs e)
        {
            MyFriendePanel.Visibility = Visibility.Visible;
            MsgPanel.Visibility = Visibility.Collapsed;
            FindPanel.Visibility = Visibility.Collapsed;
            m_status = FRIEND_PANEL;
            listFriend.ItemsSource = m_friends.ToArray();
        }

        private void btnFind_Click(object sender, RoutedEventArgs e)
        {
            MyFriendePanel.Visibility = Visibility.Collapsed;
            MsgPanel.Visibility = Visibility.Collapsed;
            FindPanel.Visibility = Visibility.Visible;
            m_status = FIND_PANEL;
            ListUser.ItemsSource = m_allUsers.ToArray();
        }

        private void BtnVisit_Click(object sender, RoutedEventArgs e)
        {
            if (m_status == FRIEND_PANEL)
                OperationManager.Instance.EnterSpace(m_friends[m_index].ID);
            else
                OperationManager.Instance.EnterSpace(m_allUsers[m_index].ID);
        }

        private void listFriend_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (listFriend.SelectedItem == null)
                return;
            m_index = listFriend.SelectedIndex;
            ShowPanel();
        }

        private void ListUser_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (ListUser.SelectedItem == null)
                return;
            m_index = ListUser.SelectedIndex;
            ShowPanel();
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            MsgPanel.Visibility = Visibility.Collapsed;
        }

        private void BtnSend_Click(object sender, RoutedEventArgs e)
        {
            if (m_status == FIND_PANEL) // make friend
            {
                if (OperationManager.Instance.MyFriends.Contains(m_allUsers[m_index].ID))
                    return;
                else if (OperationManager.Instance.MyUserInfo.UserID == m_allUsers[m_index].ID)
                    return;
                OperationManager.Instance.AddFriend(m_allUsers[m_index].ID);
                MsgPanel.Visibility = Visibility.Collapsed;
            }
            else // send message
            {
            }
        }
    }

    public class UserListData
    {
        public string ID { get; set; }
        public string NickName { get; set; }
    }
}